Imports System.ComponentModel
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Imports Phoenix.Forms.Login.Helpers

Namespace Forms.Login

    Public Class XPLoginEntry : Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call
            SetStyle(ControlStyles.ResizeRedraw, True)
            SetStyle(ControlStyles.AllPaintingInWmPaint, True)
            SetStyle(ControlStyles.UserPaint, True)
            SetStyle(ControlStyles.DoubleBuffer, True)
            SetStyle(ControlStyles.SupportsTransparentBackColor, True)
            SetStyle(ControlStyles.ContainerControl, True)
            SetStyle(ControlStyles.Selectable, True)

            MyBase.BackColor = Color.Transparent
        End Sub

        'UserControl overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Private WithEvents picNext As System.Windows.Forms.PictureBox
        Friend WithEvents txtPwd As Phoenix.Forms.Login.XPTextBox
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(XPLoginEntry))
            Me.txtPwd = New Phoenix.Forms.Login.XPTextBox
            Me.picNext = New System.Windows.Forms.PictureBox
            Me.SuspendLayout()
            '
            'txtPwd
            '
            Me.txtPwd.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.txtPwd.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
            Me.txtPwd.Location = New System.Drawing.Point(50, 32)
            Me.txtPwd.Name = "txtPwd"
            Me.txtPwd.PasswortChar = Microsoft.VisualBasic.ChrW(42)
            Me.txtPwd.Size = New System.Drawing.Size(136, 23)
            Me.txtPwd.TabIndex = 0
            Me.txtPwd.Visible = False
            '
            'picNext
            '
            Me.picNext.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.picNext.Image = CType(resources.GetObject("picNext.Image"), System.Drawing.Image)
            Me.picNext.Location = New System.Drawing.Point(190, 36)
            Me.picNext.Name = "picNext"
            Me.picNext.Size = New System.Drawing.Size(16, 16)
            Me.picNext.TabIndex = 1
            Me.picNext.TabStop = False
            Me.picNext.Visible = False
            '
            'XPLoginEntry
            '
            Me.Controls.Add(Me.picNext)
            Me.Controls.Add(Me.txtPwd)
            Me.Name = "XPLoginEntry"
            Me.Size = New System.Drawing.Size(208, 60)
            Me.ResumeLayout(False)

        End Sub

#End Region

        Public Event EnterPassword(ByVal sender As Object, ByVal e As EventArgs)
        Public Event EnterPasswordCancelled(ByVal sender As Object, ByVal e As EventArgs)
        Public Event Login(ByVal sender As Object, ByVal e As LoginEventArgs)

        Private mIcon As Image
        Private mUserName As String = ""
        Private mHelpString As String = ""
        Private mHasPwd As Boolean = False

        Private mHighlighted As Boolean = False
        Private mInactive As Boolean = False

#Region "Public Properties"
        <Description("Icon"), _
         Category("Appearance"), _
         Localizable(True), _
         DefaultValue(GetType(Icon), "")> _
        Public Property Icon() As Image
            Get
                Return mIcon
            End Get
            Set(ByVal value As Image)
                mIcon = value
                Me.Invalidate()
            End Set
        End Property

        <Description("User Name"), _
         Category("Appearance"), _
         Localizable(False), _
         DefaultValue("")> _
        Public Property UserName() As String
            Get
                Return mUserName
            End Get
            Set(ByVal value As String)
                mUserName = value
                Me.Invalidate()
            End Set
        End Property

        <Description("Help String that is displayed below User Name"), _
        Category("Appearance"), _
        Localizable(False), _
        DefaultValue("")> _
        Public Property HelpString() As String
            Get
                Return mHelpString
            End Get
            Set(ByVal value As String)
                mHelpString = value
                Me.Invalidate()
            End Set
        End Property

        <Description("flag, if control should ask for an associated password"), _
        Category("Appearance"), _
        Localizable(False), _
        DefaultValue("")> _
        Public Property HasPassword() As Boolean
            Get
                Return mHasPwd
            End Get
            Set(ByVal value As Boolean)
                mHasPwd = value
                Me.Invalidate()
            End Set
        End Property
#End Region

#Region "public methods"
        Public Sub ShowPasswordBox()
            If mHasPwd = True Then
                txtPwd.Visible = True
                picNext.Visible = True
                txtPwd.Focus()
            End If
        End Sub
#End Region

#Region "event handling"
        Protected Friend Overridable Sub OnLogin(ByVal e As LoginEventArgs)
            RaiseEvent Login(Me, e)
        End Sub

        Protected Friend Overridable Sub OnEnterPassword(ByVal e As EventArgs)
            RaiseEvent EnterPassword(Me, e)
        End Sub

        Protected Friend Overridable Sub OnEnterPasswordCancelled(ByVal e As EventArgs)
            RaiseEvent EnterPasswordCancelled(Me, e)
        End Sub
#End Region

#Region "overriden methods"
        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            MyBase.OnPaint(e)

            e.Graphics.SmoothingMode = SmoothingMode.HighQuality

            Dim gp As New GraphicsPath
            Dim stringFmt As New StringFormat

            stringFmt.Alignment = StringAlignment.Near
            stringFmt.LineAlignment = StringAlignment.Center
            stringFmt.Trimming = StringTrimming.EllipsisCharacter
            stringFmt.FormatFlags = StringFormatFlags.NoWrap

            'draw area around icon
            gp = Paths.RoundedRect(5, 5, 40, 40, 3, 1, Corner.All)
            If mHighlighted Then
                e.Graphics.FillPath(New SolidBrush(Color.Orange), gp)
            Else
                e.Graphics.FillPath(New SolidBrush(SystemColors.Highlight), gp)
            End If

            'draw icon
            If Not mIcon Is Nothing Then
                e.Graphics.DrawImage(mIcon, New Rectangle(9, 9, 32, 32))
            End If

            'draw texts
            If mUserName <> "" Then
                e.Graphics.DrawString(mUserName, New Font(MyBase.Font.FontFamily, 12, FontStyle.Regular), New SolidBrush(Me.ForeColor), 50, 1)
            End If
            If mHelpString <> "" Then
                e.Graphics.DrawString(mHelpString, New Font(MyBase.Font.FontFamily, 8, FontStyle.Regular), New SolidBrush(Me.ForeColor), 50, 19)
            End If

            gp.Dispose()
            stringFmt.Dispose()

        End Sub

        Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
            ' Change cursor to hand when over caption area.
            Dim bChanged As Boolean = False
            If e.Y <= 32 Then
                Cursor.Current = Cursors.Hand
                If mHighlighted = False Then
                    mHighlighted = True
                    bChanged = True
                End If
            Else
                Cursor.Current = Cursors.Default
                If mHighlighted = True Then
                    mHighlighted = False
                    bChanged = True
                End If
            End If

            If bChanged Then Me.Invalidate()
        End Sub

        Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
            If mHighlighted Then
                mHighlighted = False
                Me.Invalidate()
            End If
        End Sub

        Protected Overrides Sub OnResize(ByVal e As System.EventArgs)
            MyBase.OnResize(e)
            Me.Height = 60
        End Sub

        Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)
            MyBase.OnLeave(e)

            If mHasPwd = True And Not Me.ContainsFocus Then
                txtPwd.Visible = False
                picNext.Visible = False
                OnEnterPasswordCancelled(New EventArgs)
            End If
        End Sub

        Protected Overrides Sub OnClick(ByVal e As System.EventArgs)
            MyBase.OnClick(e)

            Me.Focus()
            If mHasPwd Then
                OnEnterPassword(New EventArgs)
                txtPwd.Visible = True
                picNext.Visible = True
                txtPwd.Focus()
            Else
                OnLogin(New LoginEventArgs(mUserName, txtPwd.Text, Tag))
            End If
        End Sub

#End Region

#Region "Form Events"
        Private Sub picNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picNext.Click
            OnLogin(New LoginEventArgs(mUserName, txtPwd.Text, Tag))
        End Sub


        Private Sub txtPwd_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtPwd.KeyUp
            If e.KeyCode = Keys.Enter Then
                OnLogin(New LoginEventArgs(mUserName, txtPwd.Text, Tag))
            End If
        End Sub

#End Region

    End Class

    Public Class LoginEventArgs
        Inherits EventArgs

        Private mUser As String
        Private mPassword As String
        Private mTag As Object

        Public Sub New(ByVal user As String, ByVal passwd As String, ByVal tag As Object)
            mUser = user
            mPassword = passwd
            mTag = tag
        End Sub

        Public Property User() As String
            Get
                Return mUser
            End Get
            Set(ByVal value As String)
                mUser = value
            End Set
        End Property

        Public Property Tag() As Object
            Get
                Return mTag
            End Get
            Set(ByVal value As Object)
                mTag = value
            End Set
        End Property

        Public Property Password() As String
            Get
                Return mPassword
            End Get
            Set(ByVal value As String)
                mPassword = value
            End Set
        End Property

    End Class

End Namespace